package com.bawei.user

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import androidx.databinding.adapters.TextViewBindingAdapter.setText
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.mvvm.BaseActivity
import com.bawei.user.databinding.ActivityUpdateRwdBinding
import com.blankj.utilcode.constant.RegexConstants
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/user/UpdateRwdActivity")
class UpdateRwdActivity : BaseActivity<ActivityUpdateRwdBinding,UserViewModel>() {
    override fun getLoutyId(): Int {
        return R.layout.activity_update_rwd
    }

    override fun getViewModel(): Class<UserViewModel> {
        return UserViewModel::class.java
    }

    override fun initView() {
        val tels = SPUtils.getInstance().getString("tel")
        //把4到7为****
        binding.tel.text = "当前手机号："+tels.replaceRange(3,7,"****")

        //修改密码按钮默认不可用，必须有6为验证码才可以使用
        binding.btn1.isEnabled = false
        //获取验证码
        binding.getyzm.setOnClickListener { //点击发送验证码
            //禁用按钮
            binding.getyzm.isEnabled = false
            //
            lifecycleScope.launch {
                for (i in 15 downTo 1){
                    binding.getyzm.text = "倒计时${ i}"
                    //
                    delay(1000)
                }
                //
                binding.getyzm.text = "重新发送"
                //
                binding.getyzm.isEnabled = true
            }
            //
            viewModel.sendCode(tels)
        }

        //监听验证码
        binding.yzm.addTextChangedListener {
            if (it.toString().length == 6){
                binding.btn1.isEnabled = true
            } else {
                binding.btn1.isEnabled = false
            }
        }

        //点击修改密码
        binding.btn1.setOnClickListener {
            val tv_pwd = binding.pwd.text.toString()
            val tv_okpwd = binding.okpwd.text.toString()
            //
            if (tv_okpwd.isEmpty() || tv_pwd.isEmpty()){
                ToastUtils.showLong("请输入手机号和密码")
                return@setOnClickListener
            }
            //验证手机号正则
            //RegexConstants点击此代码可以进入正则库
            if (!RegexUtils.isMatch("^[\\\\w]{8,20}(?<!_)\$",tv_pwd)){
                ToastUtils.showLong("请输入正确的密码")
                return@setOnClickListener
            }
            //
            if (tv_pwd != tv_pwd){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            //修改密码
            viewModel.updatePwd(tels,binding.yzm.text.toString(),tv_pwd)
        }
    }

    @SuppressLint("NewApi")
    override fun initDate() {

        //发送验证码成功
        viewModel.sendCodeSuccess.observe(this){
            if (it.code == 200){
                //发送通知\
                val mange = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                //退出后可以点击通知拉出应用
                val intent = Intent(this, UpdateRwdActivity::class.java)
                val pendingIntent =
                    PendingIntent.getActivity(this, 102, intent, PendingIntent.FLAG_UPDATE_CURRENT)
                //
                mange.createNotificationChannel(NotificationChannel("1","1",NotificationManager.IMPORTANCE_HIGH))
                val build = Notification.Builder(this, "1")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("验证码通知")
                    .setContentText("验证码为：${it.data.content.substring(8, 14)}")
                    //点击自动消失
                    .setAutoCancel(true)
                    //点击后可拉起app展示制定app
                    .setContentIntent(pendingIntent)
                    .build()
                //发送通知
                mange.notify(1,build)
                //设置验证码到验证码输入框中
                binding.yzm.setText(it.data.content.substring(8,14))
            }
            //
            ToastUtils.showLong(it.message)
        }

        //修改密码
        viewModel.updatePwdSuccess.observe(this){
            if (it.code == 200){
                finish()
            }
            //
            ToastUtils.showLong(it.message)
        }
    }

}